/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * frmInformasiPemesananBarangKeSupplier.java
 *
 * Created on Jan 25, 2012, 11:52:49 AM
 */
package SIMRS.Apotek.view;

import SIMRS.Apotek.sql.KoneksiDatabase;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import usu.widget.Form;
import java.text.ParseException;

/**
 *
 * @author yunniie
 */
public class frmInformasiPemesananBarangKeSupplier extends Form {
private static DefaultTableModel model;
    /** Creates new form frmInformasiPemesananBarangKeSupplier */
    public frmInformasiPemesananBarangKeSupplier() {
        initComponents();
        
        model=new DefaultTableModel();
        tblInformasiPemesananSupplier.setModel(model);
        model.addColumn("No Order");
        model.addColumn("Tanggal Order");
        model.addColumn("Ruangan");
        model.addColumn("Ruangan Tujuan");
        model.addColumn("User");
        model.addColumn("Nama Barang");
        model.addColumn("Jumlah Order");
        
        Date tanggal=new Date();
         tanggal.getDate();
        txtTanggalAwal.setDate(tanggal);
        txtTanggalAkhir.setDate(tanggal);
        
        setIconImage(new javax.swing.ImageIcon(getClass().getResource("/SIMRS/Apotek/gambar/icon.png")).getImage());
        setTitle(".:: Form Pemesanan Barang Suppplier");
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        pnlForm1 = new SIMRS.Apotek.widget.PnlForm();
        label2 = new SIMRS.Apotek.widget.Label();
        label3 = new SIMRS.Apotek.widget.Label();
        scrollPane1 = new SIMRS.Apotek.widget.ScrollPane();
        tblInformasiPemesananSupplier = new SIMRS.Apotek.widget.Table();
        label4 = new SIMRS.Apotek.widget.Label();
        label5 = new SIMRS.Apotek.widget.Label();
        btnTutup = new SIMRS.Apotek.widget.button2();
        btnCari = new SIMRS.Apotek.widget.button2();
        btnUbah = new SIMRS.Apotek.widget.button2();
        btnCetak = new SIMRS.Apotek.widget.button2();
        txtTanggalAkhir = new com.toedter.calendar.JDateChooser();
        txtTanggalAwal = new com.toedter.calendar.JDateChooser();

        setTitle("Form Informasi Pemesanan Barang Ke Supplier");
        setAnimationHide(usu.widget.constan.Animation.SHOW_FROM_BOTTOM);
        setAnimationShow(usu.widget.constan.Animation.SHOW_FROM_TOP);
        setResizable(false);
        setUndecorated(true);
        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        pnlForm1.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(57, 190, 53), 3, true));
        pnlForm1.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        label2.setText("Periode");
        pnlForm1.add(label2, new org.netbeans.lib.awtextra.AbsoluteConstraints(60, 100, 92, -1));

        label3.setText("S/d");
        pnlForm1.add(label3, new org.netbeans.lib.awtextra.AbsoluteConstraints(180, 120, -1, 21));

        tblInformasiPemesananSupplier.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        scrollPane1.setViewportView(tblInformasiPemesananSupplier);

        pnlForm1.add(scrollPane1, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 160, 610, 170));

        label4.setForeground(new java.awt.Color(255, 255, 255));
        label4.setText("Pemesanan Barang ke SUPPLIER");
        label4.setFont(new java.awt.Font("Tahoma", 1, 18));
        pnlForm1.add(label4, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 40, 400, -1));

        label5.setForeground(new java.awt.Color(255, 255, 255));
        label5.setText("Informasi ");
        label5.setFont(new java.awt.Font("Tahoma", 1, 18));
        pnlForm1.add(label5, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 20, 400, -1));

        btnTutup.setIcon(new javax.swing.ImageIcon(getClass().getResource("/SIMRS/Apotek/gambar/bt_close.png"))); // NOI18N
        btnTutup.setText("Tutup");
        btnTutup.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnTutupActionPerformed(evt);
            }
        });
        pnlForm1.add(btnTutup, new org.netbeans.lib.awtextra.AbsoluteConstraints(390, 340, -1, -1));

        btnCari.setIcon(new javax.swing.ImageIcon(getClass().getResource("/SIMRS/Apotek/gambar/bt_search.png"))); // NOI18N
        btnCari.setText("Cari");
        btnCari.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCariActionPerformed(evt);
            }
        });
        pnlForm1.add(btnCari, new org.netbeans.lib.awtextra.AbsoluteConstraints(350, 110, 100, -1));

        btnUbah.setIcon(new javax.swing.ImageIcon(getClass().getResource("/SIMRS/Apotek/gambar/bt_edit.png"))); // NOI18N
        btnUbah.setText("Ubah");
        btnUbah.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnUbahActionPerformed(evt);
            }
        });
        pnlForm1.add(btnUbah, new org.netbeans.lib.awtextra.AbsoluteConstraints(180, 340, -1, -1));

        btnCetak.setIcon(new javax.swing.ImageIcon(getClass().getResource("/SIMRS/Apotek/gambar/bt_cetak.png"))); // NOI18N
        btnCetak.setText("Cetak");
        pnlForm1.add(btnCetak, new org.netbeans.lib.awtextra.AbsoluteConstraints(70, 340, -1, -1));
        pnlForm1.add(txtTanggalAkhir, new org.netbeans.lib.awtextra.AbsoluteConstraints(210, 120, 120, -1));
        pnlForm1.add(txtTanggalAwal, new org.netbeans.lib.awtextra.AbsoluteConstraints(50, 120, 120, -1));

        getContentPane().add(pnlForm1, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, 630, 400));

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-631)/2, (screenSize.height-394)/2, 631, 394);
    }// </editor-fold>//GEN-END:initComponents

private void btnTutupActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnTutupActionPerformed
    dispose();
}//GEN-LAST:event_btnTutupActionPerformed

private void btnCariActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCariActionPerformed
// TODO add your handling code here:
    SimpleDateFormat Format=new SimpleDateFormat("yyyy-MM-dd");
    String tanggal1=Format.format(txtTanggalAwal.getDate());
    String tanggal2=Format.format(txtTanggalAkhir.getDate());
    
  model.getDataVector().removeAllElements();
    model.fireTableDataChanged();
    Connection c = KoneksiDatabase.getKoneksi();
    try {
      /*  CallableStatement callsearch=c.prepareCall("{call SearchInfoPesanBarangSupp(?,?)}");
        
        ResultSet rs=callsearch.executeQuery();*/
        
        Statement s = c.createStatement();
        String sql = "SELECT strukorder.NoOrder, strukorder.TglOrder, ruangan.NamaRuangan, strukorder.KdRuanganTujuan, strukorder.IdUser, obat.NamaBarang, detailorderruangan.JmlOrder from strukorder, ruangan, detailorderruangan, obat where (strukorder.KdRuangan=ruangan.KdRuangan) AND (strukorder.NoOrder=detailorderruangan.NoOrder) AND (obat.KdBarang=detailorderruangan.KdBarang) AND (TglOrder between '" + tanggal1 + "'and '" + tanggal2 + "') ";
        
        ResultSet rs=s.executeQuery(sql);
        while (rs.next()) {

            Object[] o = new Object[7];

            o[0] = rs.getString("NoOrder");
            o[1] = rs.getDate("TglOrder");
            o[2] = rs.getString("NamaRuangan");
            o[3] = rs.getString("KdRuanganTujuan");
            o[4] = rs.getString("IdUser");
            o[5] = rs.getString("NamaBarang");
            o[6] = rs.getString("JmlOrder");


            model.addRow(o);
        }
        
        rs.close();
    } catch (SQLException ex) {
        JOptionPane.showMessageDialog(this, "Data Tidak Ditemukan");
        
       
    } 
}//GEN-LAST:event_btnCariActionPerformed

 public void isiText(String noOrder, String ruang, String ruangTujuan, String nmBarang, String jmlh, String nmSup, Date tanggal, String tot){
  
 }

public void input() {
    int i = tblInformasiPemesananSupplier.getSelectedRow();
        if(i==-1){
            return;
        }
       
            String noOrder=tblInformasiPemesananSupplier.getValueAt(i, 0).toString();
       
        Connection c=KoneksiDatabase.getKoneksi();
         
         String ruang=tblInformasiPemesananSupplier.getValueAt(i, 2).toString();
         
         String ruangTujuan=tblInformasiPemesananSupplier.getValueAt(i, 3).toString();
         
         String idUser=tblInformasiPemesananSupplier.getValueAt(i, 4).toString();
        try {
            Statement stm=c.createStatement();
            String sql="SELECT NamaRuangan FROM ruangan where NamaRuangan = '" + ruang  + "'";
            ResultSet rsRuangan=stm.executeQuery(sql);

            rsRuangan.beforeFirst();
            while (rsRuangan.next()) {                
                 ruang= rsRuangan.getString("NamaRuangan");
            }
            rsRuangan.close();
        } catch (Exception e) {
            
        } 
        try {
            Statement stm=c.createStatement();
            String sql2="SELECT NamaRuangan FROM ruangan where KdRuangan = '" + ruangTujuan  + "'";
            ResultSet rsRuanganT=stm.executeQuery(sql2);

            rsRuanganT.beforeFirst();
            while (rsRuanganT.next()) {                
                 ruangTujuan= rsRuanganT.getString("NamaRuangan");
                
            }
            rsRuanganT.close();
        } catch (Exception e) {
            
        } 
        
        try {
            Statement stm=c.createStatement();
            String sql3="SELECT Nama FROM pegawai where IdPegawai = '" + idUser  + "'";
            ResultSet rsUser=stm.executeQuery(sql3);

            rsUser.beforeFirst();
            while (rsUser.next()) {                
                 idUser= rsUser.getString("Nama");
                
            }
            rsUser.close();
        } catch (Exception e) {
            
        } 

        SimpleDateFormat tgl = new SimpleDateFormat("yyyy-MM-dd");
                Date a = null;

                String tanggal = tblInformasiPemesananSupplier.getValueAt(i, 1).toString();
                try {
                    a = tgl.parse(tanggal);
                } catch (ParseException e) {
                   
                }
    
      String nmBarang=tblInformasiPemesananSupplier.getValueAt(i, 5).toString();
       String jmlh=tblInformasiPemesananSupplier.getValueAt(i, 6).toString();
     //  String tot=tblInformasiPemesananSupplier.getValueAt(i, 6).toString();
       
    frmPemesananKeSupplier form= new frmPemesananKeSupplier();
    form.isiText(noOrder, ruang, ruangTujuan, idUser, nmBarang, jmlh, a);
    form.setVisible(true);
}

private void btnUbahActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnUbahActionPerformed
// TODO add your handling code here:
    input();
}//GEN-LAST:event_btnUbahActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private SIMRS.Apotek.widget.button2 btnCari;
    private SIMRS.Apotek.widget.button2 btnCetak;
    private SIMRS.Apotek.widget.button2 btnTutup;
    private SIMRS.Apotek.widget.button2 btnUbah;
    private SIMRS.Apotek.widget.Label label2;
    private SIMRS.Apotek.widget.Label label3;
    private SIMRS.Apotek.widget.Label label4;
    private SIMRS.Apotek.widget.Label label5;
    private SIMRS.Apotek.widget.PnlForm pnlForm1;
    private SIMRS.Apotek.widget.ScrollPane scrollPane1;
    private SIMRS.Apotek.widget.Table tblInformasiPemesananSupplier;
    private com.toedter.calendar.JDateChooser txtTanggalAkhir;
    private com.toedter.calendar.JDateChooser txtTanggalAwal;
    // End of variables declaration//GEN-END:variables
}
